<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input
        clearable
        placeholder="请输入一体杆名称"
        class="search-main"
        v-model="params.poleName"
      />
      <span class="search-label">一体杆编号：</span>
      <el-input
        clearable
        placeholder="请输入一体杆编号"
        class="search-main"
        v-model="params.poleNumber"
      />
      <span class="search-label">运行状态：</span>
      <el-select placeholder="请选择运行状态" v-model="params.poleStatus">
        <el-option
          v-for="item in poleStatusList"
          :key="item.value"
          :value="item.value"
          :label="item.text"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="search"
        >查询</el-button
      >
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="openDialog">添加一体杆</el-button>
      <el-button @click="batchDeletePole">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="poleList"
        @selection-change="handleSelectionChange"
        :row-class-name="rowClass"
      >
        <!-- 多选 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="一体杆IP" prop="poleIp" />
        <el-table-column label="安装区域" prop="areaName" />
        <el-table-column label="一体杆类型" prop="poleType">
          <template #default="scope">
            {{ formatType(scope.row.poleType) }}
          </template>
        </el-table-column>
        <el-table-column label="运行状态" prop="poleStatus">
          <template #default="scope">
            {{ formatStatus(scope.row.poleStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editPole(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="delPole(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <!-- 添加一体杆  dialog 对话框   before-close:关闭前调用  -->
    <el-dialog title="添加一体杆" :visible.sync="isShow" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="ruleForm">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input
              :placeholder="addForm.id ? '编辑一体杆' : '添加一体杆'"
              v-model="addForm.poleName"
            />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input
              placeholder="请输入一体杆编号"
              v-model="addForm.poleNumber"
            />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input placeholder="请输入一体杆IP" v-model="addForm.poleIp" />
          </el-form-item>
          <el-form-item label="关联区域" prop="areaId">
            <!-- 关联区域 -->
            <el-select
              v-model="addForm.areaId"
              placeholder="请选择关联区域"
              style="width: 100%"
            >
              <el-option
                v-for="item in dropList"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType">
            <el-select
              v-model="addForm.poleType"
              placeholder="请选择关联区域"
              style="width: 100%"
            >
              <el-option
                v-for="item in poleTypeList"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeDialog">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPoleListAPI,
  getParkingListAPI,
  addPoleAPI,
  editPoleAPI,
  delPoleAPI
} from '@/api/pole'
export default {
  name: 'PolePage',
  data () {
    return {
      // 一体杆列表数据
      params: {
        page: 1, // 页数
        pageSize: 10, // 每页条数
        poleName: '', // 一体杆名称
        poleNumber: '', // 一体杆编号
        poleStatus: null // 一体杆运行状态
      },
      poleList: [], // 一体杆数据列表
      total: 0, // 总页数
      // 运行状态列表
      poleStatusList: [
        { text: '全部', value: null },
        { text: '正常', value: 0 },
        { text: '异常', value: 1 }
      ],
      isShow: false, // 显示隐藏对话框
      // 添加表单
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      },
      // 添加规则
      ruleForm: {
        poleName: [
          { required: true, message: '请输入一体杆名称', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '请输入一体杆编号', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入一体杆IP', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '请选择关联区域', trigger: 'change' }
        ],
        poleType: [
          { required: true, message: '请选择一体杆类型', trigger: 'change' }
        ]
      },
      // 关联地区下拉列表
      dropList: [],
      // 一体杆类型
      poleTypeList: [
        { text: '入口', value: 'center' },
        { text: '出口', value: 'export' }
      ],
      selectionData: [] // 多选框数据
    }
  },
  created () {
    this.getPoleList()
    this.getParkingList()
  },
  methods: {
    // 获取一体杆数据
    async getPoleList () {
      const res = await getPoleListAPI(this.params)
      console.log('一体杆数据', res.data.rows)
      this.poleList = res.data.rows
      this.total = res.data.total
    },
    // 页面跳转
    handleCurrentChange (val) {
      this.params.page = val
      this.getPoleList()
    },
    // 页面容量改变
    handleSizeChange (val) {
      this.params.pageSize = val
      this.getPoleList()
    },
    // 处理序号
    indexMethod (index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 搜索
    search () {
      this.params.page = 1
      this.getPoleList()
    },
    // 处理 运行状态
    formatStatus (status) {
      // console.log(status)
      const statusMap = {
        0: '正常',
        1: '异常'
      }
      return statusMap[status]
    },
    // 处理 一体杆类型
    formatType (type) {
      if (type === 'center') {
        return '入口'
      } else {
        return '出口'
      }
    },
    // 获取关联区域下拉列表
    async getParkingList () {
      const res = await getParkingListAPI()
      console.log('关联区域下拉列表', res)
      this.dropList = res.data
    },
    // 打开面板
    openDialog () {
      this.isShow = true
    },
    // 关闭面板
    closeDialog () {
      this.isShow = false
      this.$refs.addForm.resetFields()
    },
    // 提交
    submit () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        if (this.addForm.id) {
          // 编辑
          await editPoleAPI(this.addForm)
          this.$message.success('编辑成功')
        } else {
          // 添加
          await addPoleAPI(this.addForm)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getPoleList()
      })
    },
    // 编辑一体杆
    editPole (data) {
      this.openDialog()
      const { id, poleName, poleNumber, poleIp, areaId, poleType } = data
      this.addForm = { poleName, poleNumber, poleIp, areaId, poleType, id }
      console.log(this.addForm)
    },
    // 删除一体杆
    delPole (id) {
      // console.log('删除id',id)
      this.$confirm('是否确认删除当前选中的一体杆?', '温馨提示')
        .then(async () => {
          await delPoleAPI(id)
          this.$message.success('删除成功')
          this.getPoleList() // 刷新列表
        })
        .catch(() => {})
    },
    // 多选框
    handleSelectionChange (data) {
      this.selectionData = data // 给多选框数据
    },
    // 勾选数据行的 Checkbox 时触发事件
    rowClass ({ row, rowIndex }) {
      console.log(row, rowIndex)
    },
    // 批量删除
    async batchDeletePole () {
      if (this.selectionData) {
        const ids = this.selectionData.map(item => item.id)
        this.$confirm('是否确认删除当前选中的一体杆?', '温馨提示')
          .then(async () => {
            await delPoleAPI(ids)
            this.$message.success('删除成功')
            this.getPoleList() // 刷新列表
          })
          .catch(() => {})
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}

.search-label {
  font-size: 14px;
}

//table选中高亮
.el-table .slecleRowColor {
  background: #eaf4ff !important;
}
//改变element ui自带的hover高亮颜色
.el-table tbody tr:hover > td {
  background-color: #eaf4ff !important;
}
</style>
